package top.zywork.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.zywork.common.BeanUtils;
import top.zywork.common.RedisUtils;
import top.zywork.common.ReflectUtils;
import top.zywork.dao.AppAuthDAO;
import top.zywork.dos.AppAuthDO;
import top.zywork.dto.AppAuthDTO;
import top.zywork.service.AbstractBaseService;
import top.zywork.service.AppAuthService;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.List;

/**
 * AppAuthServiceImpl服务接口实现类<br/>
 *
 * 创建于2020-01-07<br/>
 *
 * @author http://zywork.top 王振宇
 * @version 1.0
 */
@Service(value = "appAuthService")
public class AppAuthServiceImpl extends AbstractBaseService implements AppAuthService {

    private static final String APP_AUTH_KEY = "app_auth::all_app_auth";

    private AppAuthDAO appAuthDAO;

    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public int save(Object dataTransferObj) {
        int saveRows = appAuthDAO.save(BeanUtils.copy(dataTransferObj, AppAuthDO.class));
        if (saveRows == 1) {
            RedisUtils.delete(redisTemplate, APP_AUTH_KEY);
        }
        return saveRows;
    }

    @Override
    public int saveBatch(List<Object> dataTransferObjList) {
        int saveRows = appAuthDAO.saveBatch(BeanUtils.copyList(dataTransferObjList, AppAuthDO.class));
        if (saveRows > 0) {
            RedisUtils.delete(redisTemplate, APP_AUTH_KEY);
        }
        return saveRows;
    }

    @Override
    public int removeById(Serializable id) {
        int deleteRow = appAuthDAO.removeById(id);
        if (deleteRow == 1) {
            RedisUtils.delete(redisTemplate, APP_AUTH_KEY);
        }
        return deleteRow;
    }

    @Override
    public int removeByIds(Serializable[] ids) {
        int deleteRows = appAuthDAO.removeByIds(ids);
        if (deleteRows > 0) {
            RedisUtils.delete(redisTemplate, APP_AUTH_KEY);
        }
        return appAuthDAO.removeByIds(ids);
    }

    @Override
    public int update(Object dataTransferObj) {
        Object idObj = ReflectUtils.getPropertyValue(dataTransferObj, "id");
        Serializable id = idObj instanceof Long ? (Long) idObj : idObj instanceof Integer ? (Integer) idObj : idObj instanceof String ? (String) idObj : 0;
        Integer version = appAuthDAO.getVersionById(id);
        version = version == null ? 1 : version;
        ReflectUtils.setPropertyValue(dataTransferObj, PROPERTY_VERSION, version + 1);
        int updateRow = appAuthDAO.update(BeanUtils.copy(dataTransferObj, AppAuthDO.class));
        if (updateRow == 1) {
            RedisUtils.delete(redisTemplate, APP_AUTH_KEY);
        }
        return updateRow;
    }

    @Override
    public int updateBatch(List<Object> dataTransferObjList) {
        int updateRows = appAuthDAO.updateBatch(BeanUtils.copyList(dataTransferObjList, AppAuthDO.class));
        if (updateRows > 0) {
            RedisUtils.delete(redisTemplate, APP_AUTH_KEY);
        }
        return updateRows;
    }

    @Override
    @SuppressWarnings({"unchecked"})
    public AppAuthDTO getAppAuthByAppid(String appid) {
        List<AppAuthDTO> appAuthDTOList = null;
        Object obj = RedisUtils.get(redisTemplate, APP_AUTH_KEY);
        if (obj != null) {
            appAuthDTOList = (List<AppAuthDTO>) obj;
        } else {
            List<Object> objList = appAuthDAO.listAll();
            if (objList != null && objList.size() > 0) {
                appAuthDTOList = BeanUtils.copy(objList, AppAuthDTO.class);
                RedisUtils.save(redisTemplate, APP_AUTH_KEY, appAuthDTOList);
            }
        }
        if (appAuthDTOList != null) {
            for (AppAuthDTO appAuthDTO : appAuthDTOList) {
                if (appid.equals(appAuthDTO.getAppId())) {
                    return appAuthDTO;
                }
            }
        }
        return null;
    }

    @Autowired
    public void setAppAuthDAO(AppAuthDAO appAuthDAO) {
        super.setBaseDAO(appAuthDAO);
        this.appAuthDAO = appAuthDAO;
    }

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @PostConstruct
    public void init() {
        super.init(AppAuthDO.class, AppAuthDTO.class);
    }
}
